package com.dp.mallchat.common.chat.service.adapter;

import com.dp.mallchat.common.chat.domain.entity.GroupMember;
import com.dp.mallchat.common.chat.domain.entity.Message;
import com.dp.mallchat.common.chat.domain.entity.Room;
import com.dp.mallchat.common.chat.domain.entity.RoomGroup;
import com.dp.mallchat.common.chat.domain.enums.MessageTypeEnum;
import com.dp.mallchat.common.chat.domain.vo.req.SendMessageReq;
import com.dp.mallchat.common.common.domain.enums.GroupRoleEnum;
import com.dp.mallchat.common.user.domain.user.entity.User;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-05  18:20
 * @Description: 房间适配类
 */
public class RoomAdapter {


    public static Room buildUpdateRoom(Room room, Message message) {
        return Room.builder()
                .id(room.getId())
                .lastMsgId(message.getId())
                .activeTime(new Date())
                .build();
    }

    public static List<GroupMember> buildGroupMemberBatch(List<Long> uidList, Long groupId) {
        return uidList.stream()
                .distinct()
                .map(uid -> {
                    GroupMember member = new GroupMember();
                    member.setRole(GroupRoleEnum.MEMBER.getType());
                    member.setUid(uid);
                    member.setGroupId(groupId);
                    return member;
                }).collect(Collectors.toList());
    }

    public static SendMessageReq buildGroupAddMessage(RoomGroup groupRoom, User inviter, Map<Long, User> member) {
        SendMessageReq chatMessageReq = new SendMessageReq();
        chatMessageReq.setRoomId(groupRoom.getRoomId());
        chatMessageReq.setMsgType(MessageTypeEnum.SYSTEM.getType());
        StringBuilder sb = new StringBuilder();
        sb.append("\"")
                .append(inviter.getName())
                .append("\"")
                .append("邀请")
                .append(member.values().stream().map(u -> "\"" + u.getName() + "\"").collect(Collectors.joining(",")))
                .append("加入群聊");
        chatMessageReq.setBody(sb.toString());
        return chatMessageReq;
    }
}
